home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / posbm.zip / POSCH.ASM < prev    next >
Assembly Source File  |  1989-06-14  |  2KB  |  77 lines

  1. ;Faster string search routine to substitute the POS()
  2. ;function (for characters only) in Turbo Pascal 4 or 5.
  3. ;
  4. ;Declare as follows:
  5. ;    {$F+}
  6. ;    {$L POSCH.OBJ}
  7. ;    FUNCTION posCH(Ch : CHAR; Str : STRING) : BYTE; EXTERNAL;
  8. ;Call as follows:
  9. ;    location := posCH(Ch, Str);
  10. ;
  11. ;Courtesy of Toad Hall
  12.  
  13. ;Total stack (caller's plus work stack)
  14. cstk    STRUC
  15. bpsave    dw    0            ;save BP here
  16. retaddr    dd    0            ;points to return address
  17. straddr    dd    0            ;points to string address
  18. chr    db    0            ;actual character
  19. cstk    ENDS
  20.  
  21. PARAMSIZE    EQU SIZE chr + SIZE straddr    ;size of parameter list
  22.  
  23. PUBLIC    PosCH                ;function name declaration
  24.  
  25. CODE    SEGMENT PARA PUBLIC 'CODE'
  26.     ASSUME    CS:CODE
  27.  
  28. ;Entry point to PosCH function
  29.  
  30. PosCH    proc    FAR
  31.     push    bp
  32.     mov    bp,sp
  33.  
  34.     mov    dx,DS            ;save caller's DS
  35.     cld
  36.  
  37. ;Get and save string text length and address
  38.     lds    si,[bp.straddr]
  39.     lodsb                ;get string length
  40.     or    al,al            ;if string text is null
  41.     jz    NoMatch            ; then exit
  42.  
  43.     mov    bx,si            ;save address ofs
  44.     xor    ah,ah            ;clear msb
  45.     mov    cx,ax            ;CX = string length
  46.     mov    al,[bp.chr]        ;snarf char
  47.  
  48.     mov    di,DS            ;DS has string's seg
  49.     mov    ES,di            ;into ES for the scas
  50.     mov    di,si            ;string offset
  51.  
  52.     repne    scasb            ;search
  53.     jnz    NoMatch            ;not found, exit
  54.  
  55. Match1:
  56.     mov    ax,di            ;just past char found
  57. ;    sub    ax,2            ;adjust DI psn
  58.  
  59.     sub    ax,bx            ;- start of strtxt
  60. ;    add    ax,2            ; in strtxt where pattern is found
  61.     jmp    short EndSearch        ;exit function
  62.  
  63. NoMatch:
  64.     xor    ax,ax            ;no match, return a 0
  65.  
  66. EndSearch:
  67.     mov    DS,dx            ;recover
  68.  
  69.     mov    sp,bp            ;recover last stack psn
  70.     pop    bp            ;recover BP
  71.     ret    PARAMSIZE        ;return with AX the posCH value
  72.  
  73. PosCH    ENDP
  74.  
  75. CODE    ENDS
  76.     END
  77.